﻿Imports System.IO
Public Class frm_TextEdit
    Private Const MaxSize As Long = 10485760
    Private isChanged As Boolean = False
    Private Delegate Sub InvokeAppendText(ByVal value As String)
    Private Delegate Sub InvokeEndRead()
    Private Delegate Sub InvokeChangeProgess(ByVal MaxValue As Long, ByVal Value As Long)
    Public FilePath As String
    Dim TextValue As New ArrayList
    Dim frmSearch As frm_SearchWindow, CurrentSelectedIndex As Integer = 0
    Const EM_GETLINECOUNT = &HBA
    Dim frmReplace As frm_SearchReplaceWindow
    Private Sub frm_TextEdit_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        SaveSetting()
        If frmReplace IsNot Nothing Then
            If frmReplace.IsDisposed = False Then
                frmReplace.Close()
            End If
        End If
        If frmSearch IsNot Nothing Then
            If frmSearch.IsDisposed = False Then
                frmSearch.Close()
            End If
        End If
        If isChanged And Not (String.IsNullOrEmpty(TextBox.Text)) Then
            If MessageBox.Show(Me, "文本内容有修改，是否先保存数据？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                Return
            Else
                SaveEditeHandle.Remove(Me)
                e.Cancel = False
            End If
        Else
            SaveEditeHandle.Remove(Me)
            e.Cancel = False
        End If
    End Sub
    Private Sub frm_Replace_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Hide()
        Me.MainMenu.Renderer = MenuSkinRender
        Me.ContextMenuObj.Renderer = MenuSkinRender
        LB_ShowProgress.Text = ""
        TextBox.AllowDrop = True
        TextBox.ShowSelectionMargin = True
        TextBox.MaxLength = 0
        SetAppTitle("无标题")
        Tool_Time.Text = DateTime.Now.ToString
        AutoCutLine(False)
        LoadSetting()
        SaveEditeHandle.Add(Me)
        SetControlPosition()
        Me.Show()

    End Sub

    Private Sub frm_Replace_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        SetControlPosition()
        If Me.WindowState = FormWindowState.Minimized Then
            If frmSearch IsNot Nothing Then
                If frmSearch.IsDisposed = False Then
                    frmSearch.Hide()
                End If
            End If
            If frmReplace IsNot Nothing Then
                If frmReplace.IsDisposed = False Then
                    frmReplace.Hide()
                End If
            End If
        ElseIf Me.WindowState = FormWindowState.Normal Then
            If frmSearch IsNot Nothing Then
                If frmSearch.IsDisposed = False Then
                    frmSearch.Show()
                End If
            End If
            If frmReplace IsNot Nothing Then
                If frmReplace.IsDisposed = False Then
                    frmReplace.Show()
                End If
            End If
        End If
    End Sub
    Private Sub SetControlPosition()
        With TextBox
            If Me.ClientRectangle.Width <= 0 Or Me.ClientRectangle.Height <= 0 Then Return
            .Left = 0
            .Top = MainMenu.Height
            .Width = Me.ClientRectangle.Width
            If SaveData.EditFormData.isShowStatus Then
                .Height = Me.ClientRectangle.Height - MainMenu.Height - StatusStrip1.Height
            Else
                .Height = Me.ClientRectangle.Height - MainMenu.Height
            End If
        End With
        Tool_Time.Text = DateTime.Now.ToString
        If (StatusStrip1.Width - Tool_Progress.Width) / 2 > 0 Then Tool_Nothing.Width = (StatusStrip1.Width - Tool_Progress.Width) / 2
        Dim Result As Integer = StatusStrip1.Width - Tool_Nothing.Width - Tool_Progress.Width - Tool_LineText.Width - Tool_Time.Width * 2 + 70
        If Result > 0 Then Tool_ColText.Width = Result
    End Sub
    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
        Dim tmp As String
        If Not String.IsNullOrEmpty(TextBox.SelectedText) Then
            tmp = TextBox.Text.Replace(TextBox.SelectedText, "")
            TextBox.Text = tmp
        End If
    End Sub

    Private Sub 剪切ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 剪切ToolStripMenuItem.Click
        TextBox.Cut()
    End Sub

    Private Sub 时间日期ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 时间日期ToolStripMenuItem.Click
        TextBox.AppendText(Now.ToString)
    End Sub

    Private Sub 全选ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 全选ToolStripMenuItem.Click
        TextBox.SelectAll()
    End Sub

    Private Sub 粘贴ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 粘贴ToolStripMenuItem.Click
        TextBox.Paste()
    End Sub
    Public Sub SetAppTitle(ByVal value As String)
        Me.Text = value & "-文本编辑器"
    End Sub

    Private Sub 新建NToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 新建NToolStripMenuItem.Click
        Dim frm As New frm_TextEdit
        frm.Show()
    End Sub
    Private Sub AutoCutLine(ByVal value As Boolean)
        If value = True Then
            TextBox.WordWrap = True
            TextBox.ScrollBars = RichTextBoxScrollBars.ForcedVertical
        Else
            TextBox.WordWrap = False
            TextBox.ScrollBars = RichTextBoxScrollBars.ForcedBoth
        End If
    End Sub
    Private Sub 打开ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开ToolStripMenuItem.Click
        Dim dialog As New OpenFileDialog
        If isChanged And Not (String.IsNullOrEmpty(TextBox.Text)) Then
            If MessageBox.Show(Me, "文本内容有改变，是否保存文档！", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                Return
            Else
                TextBox.Clear()
            End If
        End If
        With dialog
            .Title = "打开文档"
            .Filter = "文档类型(*.txt;*.dat;*.rtf;*.ini;*.html)|*.txt;*.dat;*.rtf;*.ini;*.html|所有文件(*.*)|*.*"
            .FileName = ""
            .CheckFileExists = True

            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim typeStr As String = "." & getOnlyType(.FileName)
                If "*.txt;*.dat;*.ini;*.html".IndexOf(typeStr.ToLower) > 0 Then
                    Me.打开ToolStripMenuItem.Enabled = False
                    Dim th As Threading.Thread
                    th = New Threading.Thread(AddressOf StartReadText)
                    th.Start(.FileName)
                ElseIf typeStr.ToLower = ".rtf" Then
                    TextBox.LoadFile(.FileName, RichTextBoxStreamType.RichText)
                End If
                FilePath = .FileName
                SetAppTitle(getOnlyName(.FileName))
            End If
        End With
    End Sub
    Private Sub StartReadText(ByVal FileName As String)
        Try
            If File.Exists(FileName) Then
                Dim ReadByte() As Byte = Nothing
                Dim fs As New FileStream(FileName, FileMode.Open, FileAccess.Read)
                If fs.Length / 1024 / 1024 > 60 Then Throw New Exception("文件大于支持的大小，最大支持60M！")
                TextValue.Clear()
                If fs.Length > MaxSize Then
                    Dim StartP As Long = 0
                    Dim sCount As Integer = 0
                    Dim Result As String = ""
                    Dim lCount As Integer = fs.Length \ MaxSize
                    If fs.Length Mod MaxSize <> 0 Then lCount = lCount + 1
                    Me.Invoke(New InvokeChangeProgess(AddressOf ChanagProgress), lCount, 0)
                    Do Until fs.Position >= fs.Length
                        If StartP + MaxSize < fs.Length Then
                            'Try
                            ReDim ReadByte(MaxSize - 1)
                            fs.Read(ReadByte, 0, MaxSize)
                            Result = TranslateByte(ReadByte)
                            sCount += 1
                            If sCount <= 1 Then

                                TextValue.Add(Result)
                            Else
                                TextValue.Add(Result)
                            End If
                        Else
                            Dim LeftLen As Long = fs.Length - StartP + 1
                            ReDim ReadByte(LeftLen - 1)
                            fs.Read(ReadByte, StartP, LeftLen)
                            TranslateByte(ReadByte)
                            StartP = StartP + LeftLen
                            TextValue.Add(Result)
                        End If
                        Try
                            Me.Invoke(New InvokeAppendText(AddressOf AppendText), Result)
                        Catch
                            MsgBox((sCount - 1) * MaxSize / 1024 / 1024)
                        End Try
                        Me.Invoke(New InvokeChangeProgess(AddressOf ChanagProgress), lCount, sCount)
                    Loop

                Else
                    Dim Result As String
                    ReDim ReadByte(fs.Length - 1)
                    fs.Read(ReadByte, 0, fs.Length)
                    Result = TranslateByte(ReadByte)
                    Me.Invoke(New InvokeAppendText(AddressOf AppendText), Result)
                    TextValue.Add(Result)
                End If
                fs.Close()
                Me.Invoke(New InvokeEndRead(AddressOf EndRead))
            End If
            Try
                isChanged = False
                Dim gth As Threading.Thread = Threading.Thread.CurrentThread
                gth.Abort()
            Catch
                Try
                    Dim gth As Threading.Thread = Threading.Thread.CurrentThread
                    gth.Abort()
                Catch

                End Try
            End Try
        Catch ex As Exception
            If ex.Message <> "正在中止线程。" Then MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Try
                Dim gth As Threading.Thread = Threading.Thread.CurrentThread
                gth.Abort()
            Catch

            End Try
        End Try
    End Sub
    Private Sub ChanagProgress(ByVal MaxValue As Long, ByVal Value As Long)
        LB_ShowProgress.Text = Format(Value / MaxValue * 100, "00") & "%"
        If Value / MaxValue = 1 Then LB_ShowProgress.Text = ""
    End Sub
    Private Sub EndRead()
        Me.打开ToolStripMenuItem.Enabled = True
        LB_ShowProgress.Text = ""
        MessageBox.Show(Me, "数据读取结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub
    Private Function TranslateByte(ByVal Data As Byte()) As String
        Dim tmp As String
        tmp = System.Text.Encoding.Default.GetString(Data)
        Return tmp
    End Function
    Private Sub AppendText(ByVal Value As String)
        TextBox.AppendText(Value)
    End Sub
    Private Sub 字体ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 字体ToolStripMenuItem.Click
        Dim fDialog As New FontDialog
        With fDialog
            .AllowScriptChange = True
            .AllowVectorFonts = True
            .AllowVerticalFonts = True
            .FontMustExist = True
            .ShowApply = True
            .ShowColor = True
            .ShowEffects = True
            .Color = TextBox.ForeColor
            .Font = TextBox.Font
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                TextBox.SelectionColor = .Color
                TextBox.SelectionFont = .Font
            End If
        End With
    End Sub

    Private Sub 字体颜色ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 字体颜色ToolStripMenuItem.Click
        Dim dcolor As New ColorDialog
        dcolor.AnyColor = True
        dcolor.SolidColorOnly = False
        If dcolor.ShowDialog() = Windows.Forms.DialogResult.OK Then
            TextBox.ForeColor = dcolor.Color
        End If
    End Sub

    Private Sub 自动换行ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 自动换行ToolStripMenuItem.Click
        自动换行ToolStripMenuItem.Checked = Not 自动换行ToolStripMenuItem.Checked
        With TextBox
            If 自动换行ToolStripMenuItem.Checked Then
                .ScrollBars = RichTextBoxScrollBars.ForcedVertical
                .WordWrap = True
            Else
                .ScrollBars = RichTextBoxScrollBars.ForcedBoth
                .WordWrap = False
            End If
        End With
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Tool_Time.Text = DateTime.Now.ToString
    End Sub

    Private Sub 状态栏ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 状态栏ToolStripMenuItem.Click
        状态栏ToolStripMenuItem.Checked = Not 状态栏ToolStripMenuItem.Checked
        StatusStrip1.Visible = 状态栏ToolStripMenuItem.Checked
        If StatusStrip1.Visible = True Then
            TextBox.Height = Me.ClientRectangle.Height - MainMenu.Height - StatusStrip1.Height
        Else
            TextBox.Height = Me.ClientRectangle.Height - MainMenu.Height
        End If
    End Sub

    Private Sub TextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox.DragDrop
        With TextBox
            If Not String.IsNullOrEmpty(.Text) And isChanged Then
                If MessageBox.Show(Me, "是否保存已有内容？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    保存SToolStripMenuItem_Click(sender, New EventArgs)
                    .Text = ""
                Else
                    .Text = ""
                End If
            Else
                .Text = ""
            End If
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                Dim FileName As String() = e.Data.GetData(DataFormats.FileDrop, False)
                If FileName.Length > 2 Then MessageBox.Show(Me, "一次只能拖动一个文件！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) : Return
                'For i = 0 To FileName.Length - 1
                '    TextBox.AppendText(vbCrLf & FileName(i))
                'Next
                Dim sFileName As String = FileName(0)
                Dim typeStr As String = "." & getOnlyType(sFileName)
                If "*.txt;*.dat;*.ini;*.html".IndexOf(typeStr.ToLower) > 0 Then
                    Me.打开ToolStripMenuItem.Enabled = False
                    Dim th As Threading.Thread
                    th = New Threading.Thread(AddressOf StartReadText)
                    th.Start(sFileName)
                ElseIf typeStr.ToLower = ".rtf" Then
                    TextBox.LoadFile(sFileName, RichTextBoxStreamType.RichText)
                Else
                    MessageBox.Show(Me, "不支持该文件格式！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Return
                End If
                FilePath = sFileName
                SetAppTitle(getOnlyName(sFileName))
            ElseIf e.Data.GetDataPresent(DataFormats.Html) Then
                .AppendText(e.Data.GetData(DataFormats.UnicodeText))
            End If
        End With
    End Sub

    Private Sub TextBox_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArgs) Handles TextBox.LinkClicked
        If MessageBox.Show(Me, "是否打开点击网页？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            Process.Start(e.LinkText)
        End If
    End Sub

    Private Sub TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox.LostFocus

    End Sub

    Private Sub TextBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox.MouseDown

    End Sub
    Public Sub LoadSetting()
        '==================load from local data
        Me.状态栏ToolStripMenuItem.Checked = SaveData.EditFormData.isShowStatus
        Me.自动换行ToolStripMenuItem.Checked = SaveData.EditFormData.isWordWrap
        If SaveData.EditFormData.formWindowState = FormWindowState.Maximized Then
            Me.WindowState = FormWindowState.Maximized
        ElseIf SaveData.EditFormData.formWindowState = FormWindowState.Minimized Then
            Me.WindowState = FormWindowState.Minimized
        Else
            Me.WindowState = FormWindowState.Normal
            If SaveData.EditFormData.frmX > 0 Then Me.Left = SaveData.EditFormData.frmX
            If SaveData.EditFormData.frmY > 0 Then Me.Top = SaveData.EditFormData.frmY
            If SaveData.EditFormData.frmHeight > 0 Then Me.Height = SaveData.EditFormData.frmHeight
            If SaveData.EditFormData.frmWidth > 0 Then Me.Width = SaveData.EditFormData.frmWidth
        End If
        With SaveData.EditFormData
            If .TextFont IsNot Nothing Then TextBox.Font = .TextFont
            TextBox.ForeColor = .TextColor
        End With
        '=========================================
        StatusStrip1.Visible = SaveData.EditFormData.isShowStatus
        If SaveData.EditFormData.isShowStatus Then
            TextBox.Height = Me.ClientRectangle.Height - MainMenu.Height - StatusStrip1.Height
        Else
            TextBox.Height = Me.ClientRectangle.Height - MainMenu.Height
        End If
        With TextBox
            If 自动换行ToolStripMenuItem.Checked Then
                .ScrollBars = RichTextBoxScrollBars.ForcedVertical
                .WordWrap = True
            Else
                .ScrollBars = RichTextBoxScrollBars.ForcedBoth
                .WordWrap = False
            End If
        End With
    End Sub
    Private Sub SaveSetting()
        With SaveData.EditFormData
            .formWindowState = Me.WindowState
            .frmHeight = Me.Height
            .frmWidth = Me.Width
            .frmX = Me.Left
            .frmY = Me.Top
            .isWordWrap = Me.自动换行ToolStripMenuItem.Checked
            .isShowStatus = Me.状态栏ToolStripMenuItem.Checked
            .TextFont = TextBox.Font
            .TextColor = TextBox.ForeColor
        End With
    End Sub

    Private Sub TextBox_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox.SelectionChanged
        Dim pos = TextBox.GetPositionFromCharIndex(TextBox.SelectionStart)
        Dim x = TextBox.GetCharIndexFromPosition(pos)
        Dim y = TextBox.GetLineFromCharIndex(TextBox.SelectionStart)
        Dim a = TextBox.GetFirstCharIndexOfCurrentLine
        Tool_LineText.Text = "行：" & y + 1
        Tool_ColText.Text = "列：" & x - a + 1
    End Sub
    Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox.TextChanged
        isChanged = True
        Tool_Progress.Text = "总行数:" & getLineCount()
    End Sub

    Private Sub 复制ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 复制ToolStripMenuItem.Click
        Clipboard.SetText(TextBox.SelectedText)
    End Sub

    Private Sub 撤销ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 撤销ToolStripMenuItem.Click
        TextBox.Undo()
    End Sub

    Private Sub 重复RToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 重复RToolStripMenuItem.Click
        TextBox.Redo()
    End Sub

    Private Sub 保存SToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 保存SToolStripMenuItem.Click
        If File.Exists(FilePath) = False Then
            Dim Dialog As New SaveFileDialog
            With Dialog
                .Title = "保存"
                .Filter = "文本文档(*.txt)|*.txt|富文本(*.rtf)|*.rtf|配置文件(*.ini)|*.ini|网页文件(*.html)|*.html|*.dat(*.dat)|*.dat"
                .CheckPathExists = True
                .DefaultExt = "*.txt"
                If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                    FilePath = .FileName
                Else
                    FilePath = ""
                    Return
                End If
            End With
        End If
        SaveFile()
    End Sub
    Private Sub SaveFile()
        isChanged = False
        Dim typeStr As String = getOnlyType(FilePath)
        If String.IsNullOrEmpty(typeStr) Then
            TextBox.SaveFile(FilePath, RichTextBoxStreamType.TextTextOleObjs)
        Else
            Select Case typeStr.ToLower
                Case "txt"
                    TextBox.SaveFile(FilePath, RichTextBoxStreamType.TextTextOleObjs)
                Case "rtf"
                    TextBox.SaveFile(FilePath, RichTextBoxStreamType.RichText)
                Case "ini"
                    TextBox.SaveFile(FilePath, RichTextBoxStreamType.TextTextOleObjs)
                Case "dat"
                    TextBox.SaveFile(FilePath, RichTextBoxStreamType.TextTextOleObjs)
                Case "html"
                    TextBox.SaveFile(FilePath, RichTextBoxStreamType.TextTextOleObjs)
            End Select
        End If
        SetAppTitle(getOnlyName(FilePath))
    End Sub

    Private Sub 另存为AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 另存为AToolStripMenuItem.Click
        Dim Dialog As New SaveFileDialog
        With Dialog
            .Title = "保存"
            .Filter = "文本文档(*.txt)|*.txt|富文本(*.rtf)|*.rtf|配置文件(*.ini)|*.ini|网页文件(*.html)|*.html|*.dat(*.dat)|*.dat"
            .CheckPathExists = True
            .DefaultExt = "*.txt"
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                FilePath = .FileName
                SaveFile()
            Else
                '  FilePath = ""
                Return
            End If
        End With
    End Sub

    Private Sub 退出EscToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出EscToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub 关于ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 关于ToolStripMenuItem.Click
        MessageBox.Show(Me, "名  称：文本编辑器" & vbCrLf & _
                           "版  本：v1.0.0.0" & vbCrLf & _
                          "作  者：莫书江(e_mail:250933400@qq.com)", "关于", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub 撤销ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 撤销ToolStripMenuItem1.Click
        Me.撤销ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub 复制ToolStripMenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 复制ToolStripMenuItem1.Click
        复制ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub 重复RToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 重复RToolStripMenuItem1.Click
        Me.重复RToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub 剪切CToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 剪切CToolStripMenuItem.Click
        Me.剪切ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub 粘贴PToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 粘贴PToolStripMenuItem.Click
        粘贴ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub 删除DToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 删除DToolStripMenuItem.Click
        删除ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub 全选AToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles 全选AToolStripMenuItem.Click
        全选ToolStripMenuItem_Click(sender, e)
    End Sub

    Private Sub 日期ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 日期ToolStripMenuItem.Click
        时间日期ToolStripMenuItem_Click(sender, e)

    End Sub

    Private Sub TextBox_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox.VScroll
        Dim sPt As New Point(0, 0)
        Dim a As Long
        With TextBox
            a = .GetCharIndexFromPosition(sPt)
        End With
    End Sub

    Private Sub 查找ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 查找ToolStripMenuItem.Click
        If frmSearch Is Nothing Then frmSearch = New frm_SearchWindow(Me, TextBox.SelectedText)
        If frmSearch.IsDisposed Then frmSearch = New frm_SearchWindow(Me, TextBox.SelectedText)
        '  SetParent(frmSearch.Handle.ToInt32, Me.Handle.ToInt32)
        frmSearch.Left = Me.Left + (Me.Width - frmSearch.Width) / 2
        frmSearch.Top = Me.Top + (Me.Height - frmSearch.Height) / 2
        frmSearch.TopMost = True
        frmSearch.FatherForm = Me
        frmSearch.Show()
        If frmReplace IsNot Nothing Then If frmReplace.IsHandleCreated Then frmReplace.Close()
    End Sub
    Public Function getLineCount() As Long
        Return sAPI.SendMessage(TextBox.Handle.ToInt32, EM_GETLINECOUNT, 0, 0)
    End Function
   
    Private Sub 替换RToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 替换RToolStripMenuItem.Click
        If frmReplace Is Nothing Then frmReplace = New frm_SearchReplaceWindow(TextBox.SelectedText)
        If frmReplace.IsDisposed Then frmReplace = New frm_SearchReplaceWindow(TextBox.SelectedText)
        frmReplace.FatherForm = Me
        With frmReplace
            .Left = Me.Left + (Me.Width - frmReplace.Width) / 2
            .Top = Me.Top + (Me.Height - frmReplace.Height) / 2
            frmReplace.TopMost = True
            .Show()
            If frmSearch IsNot Nothing Then If frmSearch.IsHandleCreated Then frmSearch.Close()
        End With
    End Sub
End Class